﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class Prijavljivanje : Form
    {

        MySqlDataReader rdr = null;
        MySqlConnection conn = new MySqlConnection("SERVER='etf.ba';" + "DATABASE='ooadtim8';" + "UID='ooadtim8';" + "PASSWORD='ooadtim8';");
        public Prijavljivanje()
        {
            InitializeComponent();
        }
       
        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Brush BackBrush = new SolidBrush(Color.AntiqueWhite);
            Brush ForeBrush = new SolidBrush(Color.Salmon);
            Font TabFont = new Font("Segoe Print", 10, FontStyle.Regular);
            string TabName = this.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(BackBrush, e.Bounds);
            Rectangle r = e.Bounds;
            r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
            e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
            sf.Dispose();
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                TabFont.Dispose();
                BackBrush.Dispose();
            }
            else
            {
                BackBrush.Dispose();
                ForeBrush.Dispose();
            }
        }

       private void tabPage1_Paint(object sender, PaintEventArgs e)
        {
            this.pictureBox1.Image = global::ElektronskiDnevnik.Properties.Resources.black_and_white_children_couple_cute_photography_Favim_com_43897;
        }

        private void tabPage2_Paint(object sender, PaintEventArgs e)
        {
            this.pictureBox1.Image = global::ElektronskiDnevnik.Properties.Resources.Kara_Heck_600x428;
          
        }

        private void tabPage3_Paint(object sender, PaintEventArgs e)
        {
            this.pictureBox1.Image = global::ElektronskiDnevnik.Properties.Resources.blogimages;
        }
      
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Segoe Print", 48, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.Salmon);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Elektronski dnevnik", font, brush, 130, 25);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
                MessageBox.Show("Niste unijeli sve potrebne podatke", "Upozorenje", MessageBoxButtons.OK, 
                    MessageBoxIcon.Warning);
            else
            {
                MySqlCommand cmd = new MySqlCommand("SELECT * FROM Osoba WHERE JMBG = '" + textBox1.Text + " ' ", 
                    conn);
                try
                {
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    rdr = cmd.ExecuteReader();
                    if (rdr.Read())
                    {
                        if (rdr.GetValue(1).ToString() == textBox2.Text)
                        {
                            if (Convert.ToInt32(rdr.GetValue(2)) == 1)
                            {
                                this.Hide();
                                IUcenik nova = new IUcenik(this, textBox1.Text);
                                nova.Show();
                                textBox1.Clear();
                                textBox2.Clear();
                            }
                            if (Convert.ToInt32(rdr.GetValue(2)) == 2)
                            {
                                this.Hide();
                                IRoditelj nova = new IRoditelj(this, textBox1.Text);
                                nova.Show();
                                textBox1.Clear();
                                textBox2.Clear();
                            }
                            if (Convert.ToInt32(rdr.GetValue(2)) == 3)
                            {
                                this.Hide();
                                INastavnik nova = new INastavnik(this, textBox1.Text);
                                nova.Show();
                                textBox1.Clear();
                                textBox2.Clear();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Unijeli ste pogresan password.", "Upozorenje", MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                            textBox2.Clear();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ne postoji korisnik sa korisnickim imenom koje ste unijeli.", "Upozorenje",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        textBox1.Clear();
                        textBox1.Clear();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
                    {
                        button1.Enabled = false;
                        button2.Enabled = false;
                        button3.Visible = true;
                    }
                }
                finally
                {
                    if (rdr != null)
                        rdr.Close();
                    if (conn != null)
                        conn.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
                {
                    button1.Enabled = false;
                    button2.Enabled = false;
                    button3.Visible = true;
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
                {
                    button1.Enabled = false;
                    button2.Enabled = false;
                    button3.Visible = true;
                }
            }
            if (conn != null)
            {
                String mySelect = "SELECT * FROM Skola;";
                try
                {
                    MySqlCommand myCommand = new MySqlCommand(mySelect, conn);
                    rdr = myCommand.ExecuteReader();
                    rdr.Read();
                    textBox3.Text = Convert.ToString(rdr["Naziv"]);
                    textBox4.Text = Convert.ToString(rdr["Mjesto"]);
                    textBox5.Text = Convert.ToString(rdr["Opcina"]);
                    String ulica;
                    ulica = Convert.ToString(rdr["Ulica"]);
                    int broj = Convert.ToInt32(rdr["Broj"]);
                    ulica = ulica + " br. " + Convert.ToString(broj);
                    textBox6.Text = ulica;
                    textBox7.Text = Convert.ToString(rdr["Telefon"]);
                    textBox8.Text = Convert.ToString(rdr["Email"]);
                    textBox9.Text = Convert.ToString(rdr["Kanton"]);
                    rdr.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                timerPokreni_Tick(this, null);
            }
            
        }

        private void timerPokreni_Tick(object sender, EventArgs e)
        {
            Thread check = new Thread(new ThreadStart(ProvjeriBazu));
            check.Start();
            timerPokreni.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("Dovidjenja :)", "Izlaz", MessageBoxButtons.OK, MessageBoxIcon.None);
            conn.Close();
        }

        private void ProvjeriBazu()
        {
            try
            {
                if (conn.State == ConnectionState.Closed) conn.Open();
                MySqlCommand myCommand = new MySqlCommand("SELECT * FROM PotvrdaMolba;", conn);
                MySqlDataReader myReader;
                myReader = myCommand.ExecuteReader();
                List<PotvrdaMolba> potvrdeMolbe = new List<PotvrdaMolba>();
                while (myReader.Read())
                {
                    int tip = Convert.ToInt32(myReader.GetValue(2));
                    string _brojProtokola = Convert.ToString(myReader.GetValue(0));
                    DateTime _datumPodnosenja = Convert.ToDateTime(myReader.GetValue(1));
                    int _status = Convert.ToInt32(myReader.GetValue(3));
                    string _svrha = Convert.ToString(myReader.GetValue(4));
                    string _ucenik = Convert.ToString(myReader.GetValue(5));
                    string _podnosioc = Convert.ToString(myReader.GetValue(6));
                    PotvrdaMolba potvrda = new RedovnoSkolovanje(_brojProtokola, _datumPodnosenja,
                                                    _status, _svrha, _ucenik, _podnosioc, tip);
                    potvrdeMolbe.Add(potvrda);
                }

                myReader.Close();

                foreach (PotvrdaMolba p in potvrdeMolbe)
                {
                    TimeSpan t = new TimeSpan(3, 0, 0, 0);
                    if (DateTime.Now - p.DatumPodnosenja > t && Convert.ToInt32(p.Status) == 1)
                    {
                        myCommand = new MySqlCommand("UPDATE PotvrdaMolba SET Rijesenje = 3 WHERE BrojProtokola='" +
                            p.BrojProtokola + "';", conn);
                        myCommand.ExecuteNonQuery();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
